/**
 * The MIT License (MIT)
 * 九宫格抽奖
 * @author 透笔度
 * @开源中国 https://my.oschina.net/tbd/blog
 * @码云 https://gitee.com/dgx
 */

const towxImg = require('../../../utils/towxImg');
// pages/jiugongge3/jiugongge3.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Jack_pots_select: [false, false, false, false, false, false, false, false],
    Jack_pots_val: ["空调", "未中奖", "洗衣机", "未中奖", "娃哈哈", "电脑", "未中奖", "手机"],
    is_play: false,//是否在运动中，避免重复启动bug
    available_num: 3,//可用抽奖的次数，可自定义设置或者接口返回
    start_position: 0,//转动开始时首次点亮的位置，可自定义设置
    base_circle_num: 5,//基本圈数，就是在转到（最后一圈）结束圈之前必须转够几圈 ，可自定义设置
    low_circle_num: 4,//在第几圈开始进入减速圈（必须小于等于基本圈数），可自定义设置
    use_speed: 50,//当前速度，与正常转速值相等
    nor_speed: 50,//正常转速，在减速圈之前的转速，可自定义设置
    low_speed: 120,//减速转速，在减速圈的转速，可自定义设置
    end_speed: 250,//最后转速，在结束圈的转速，可自定义设置
    random_number: 0,//中奖索引，也是随机数，也是结束圈停止的位置，这个值采用系统随机或者接口返回
    change_num: 0,//变化计数，0开始，比如实例有12个奖项，基本是6圈，那么到结束这个值=6*12+random_number；同样change_num/12整除表示走过一整圈
    result_val: "未中奖"//存放奖项容器，可自定义设置
  },
  //启动
  start: function () {
    var that = this;
    //是否用完可用抽奖次数
    //console.log(that.data.available_num);
    if (that.data.available_num < 1) {
      //code

      return false;
    }
    //阻止运动中重复点击
    if (!that.data.is_play) {
      //设置标识在运动中
      that.setData({ is_play: true });
      //重置参数
      that.reset();
      //生成随机奖项索引(0-that.data.Jack_pots.length)之间 || 或者后台返回这个获奖内容
      //方式1 后台
      // ajax({
      //   url,
      //   succ:function(res){
      //     // res.random来自后台的中奖索引
      //     that.setData({ random_number: res.random });
      //   }
      // })
      //方式2 随机
      that.setData({ random_number: Math.floor(Math.random() * that.data.Jack_pots_val.length) });
      //console.log(that.data.random_number);
      //运动函数
      setTimeout(that.dong, that.data.use_speed);
    };

  },
  //运动函数
  dong: function () {
    var that = this;
    //状态
    var status = [];
    for (var j = 0; j < that.data.Jack_pots_val.length; j++) {
      status[j] = false;
    };
    //重置显示
    that.setData({ Jack_pots_select: status });
    //点亮
    status[that.data.change_num % that.data.Jack_pots_val.length] = true;
    that.setData({ Jack_pots_select: status });
    //累加变化计数
    that.setData({ change_num: that.data.change_num + 1 });
    //继续运动
    if (that.data.change_num > that.data.base_circle_num * that.data.Jack_pots_val.length + that.data.random_number) {//已经到达结束位置
      //提示中奖，
      // console.log(that.data.Jack_pots_val[that.data.random_number])
      that.setData({ result_val: that.data.Jack_pots_val[that.data.random_number] })
      //code

      //运动结束设置可用抽奖的次数和激活状态设置可用
      that.endset();


    } else {//运动
      //console.log(that.data.change_num)
      if (that.data.change_num / that.data.Jack_pots_val.length + 1 < that.data.low_circle_num) {//正常转速
        //console.log("正常转速")
        that.data.use_speed = that.data.nor_speed
      } else if (that.data.change_num / that.data.Jack_pots_val.length + 1 >= that.data.low_circle_num && that.data.change_num / that.data.Jack_pots_val.length + 1 <= that.data.base_circle_num + 1) { //减速圈
        //console.log("减速圈")
        that.data.use_speed = that.data.low_speed
      } else if (that.data.change_num / that.data.Jack_pots_val.length + 1 > that.data.base_circle_num + 1) { //结束圈
        //console.log("结束圈")
        that.data.use_speed = that.data.end_speed
      }
      setTimeout(that.dong, that.data.use_speed);
    }

  },
  //运动结束设置可用抽奖的次数和激活状态设置可用
  endset: function () {
    var that = this;
    //是否在运动中，避免重复启动bug
    that.setData({ is_play: false })
    //可用抽奖的次数，可自定义设置
    that.setData({ available_num: that.data.available_num - 1 });
  },
  //重置参数
  reset: function () {
    var that = this;
    //转动开始时首次点亮的位置，可自定义设置
    that.setData({ start_position: 0 });
    //当前速度，与正常转速值相等
    that.setData({ use_speed: that.data.nor_speed });
    //中奖索引，也是随机数，也是结束圈停止的位置，这个值采用系统随机或者接口返回
    that.setData({ random_number: 0 });
    //变化计数，0开始，必须实例有12个奖项，基本是6圈，那么到结束这个值=6*12+random_number；同样change_num/12整除表示走过一整圈
    that.setData({ change_num: 0 });

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let code = '```\n<view class="container my-page">\n  <view>九宫格大转盘</view>\n  <!--九宫格大转盘-->\n  <view id="lottery">\n    <view class="lottery-unit lottery-unit-0 {{Jack_pots_select[0]?"select":""}}">{{Jack_pots_val[0]}}</view>\n    <view class="lottery-unit lottery-unit-1 {{Jack_pots_select[1]?"select":""}}">{{Jack_pots_val[1]}}</view>\n    <view class="lottery-unit lottery-unit-2 {{Jack_pots_select[2]?"select":""}}">{{Jack_pots_val[2]}}</view>\n    <view class="lottery-unit lottery-unit-3 {{Jack_pots_select[3]?"select":""}}">{{Jack_pots_val[3]}}</view>\n    <view class="lottery-unit lottery-unit-4 {{Jack_pots_select[4]?"select":""}}">{{Jack_pots_val[4]}}</view>\n    <view class="lottery-unit lottery-unit-5 {{Jack_pots_select[5]?"select":""}}">{{Jack_pots_val[5]}}</view>\n    <view class="lottery-unit lottery-unit-6 {{Jack_pots_select[6]?"select":""}}">{{Jack_pots_val[6]}}</view>\n    <view class="lottery-unit lottery-unit-7 {{Jack_pots_select[7]?"select":""}}">{{Jack_pots_val[7]}}</view>\n    <view class="start" id="start" bindtap="start">抽奖</view>\n  </view>\n  <!--九宫格大转盘-->\n  <view>\n    获奖结果：{{result_val}}\n  </view>\n  <view>\n    剩余抽奖次数：{{available_num}}\n  </view>\n</view>\n```\n\n```\nPage({\n  /**\n   * 页面的初始数据\n   */\n  data: {\n    Jack_pots_select: [false, false, false, false, false, false, false, false],\n    Jack_pots_val: ["空调", "未中奖", "洗衣机", "未中奖", "娃哈哈", "电脑", "未中奖", "手机"],\n    is_play: false,//是否在运动中，避免重复启动bug\n    available_num: 3,//可用抽奖的次数，可自定义设置或者接口返回\n    start_position: 0,//转动开始时首次点亮的位置，可自定义设置\n    base_circle_num: 5,//基本圈数，就是在转到（最后一圈）结束圈之前必须转够几圈 ，可自定义设置\n    low_circle_num: 4,//在第几圈开始进入减速圈（必须小于等于基本圈数），可自定义设置\n    use_speed: 50,//当前速度，与正常转速值相等\n    nor_speed: 50,//正常转速，在减速圈之前的转速，可自定义设置\n    low_speed: 120,//减速转速，在减速圈的转速，可自定义设置\n    end_speed: 250,//最后转速，在结束圈的转速，可自定义设置\n    random_number: 0,//中奖索引，也是随机数，也是结束圈停止的位置，这个值采用系统随机或者接口返回\n    change_num: 0,//变化计数，0开始，比如实例有12个奖项，基本是6圈，那么到结束这个值=6*12+random_number；同样change_num/12整除表示走过一整圈\n    result_val: "未中奖"//存放奖项容器，可自定义设置\n  },\n  //启动\n  start: function () {\n    var that = this;\n    //是否用完可用抽奖次数\n    //console.log(that.data.available_num);\n    if (that.data.available_num < 1) {\n      //code\n      return false;\n    }\n    //阻止运动中重复点击\n    if (!that.data.is_play) {\n      //设置标识在运动中\n      that.setData({ is_play: true });\n      //重置参数\n      that.reset();\n      //生成随机奖项索引(0-that.data.Jack_pots.length)之间 || 或者后台返回这个获奖内容\n      //方式1 后台\n      // ajax({\n      //   url,\n      //   succ:function(res){\n      //     // res.random来自后台的中奖索引\n      //     that.setData({ random_number: res.random });\n      //   }\n      // })\n      //方式2 随机\n      that.setData({ random_number: Math.floor(Math.random() * that.data.Jack_pots_val.length) });\n      //console.log(that.data.random_number);\n      //运动函数\n      setTimeout(that.dong, that.data.use_speed);\n    };\n  },\n  //运动函数\n  dong: function () {\n    var that = this;\n    //状态\n    var status = [];\n    for (var j = 0; j < that.data.Jack_pots_val.length; j++) {\n      status[j] = false;\n    };\n    //重置显示\n    that.setData({ Jack_pots_select: status });\n    //点亮\n    status[that.data.change_num % that.data.Jack_pots_val.length] = true;\n    that.setData({ Jack_pots_select: status });\n    //累加变化计数\n    that.setData({ change_num: that.data.change_num + 1 });\n    //继续运动\n    if (that.data.change_num > that.data.base_circle_num * that.data.Jack_pots_val.length + that.data.random_number) {//已经到达结束位置\n      //提示中奖，\n      // console.log(that.data.Jack_pots_val[that.data.random_number])\n      that.setData({ result_val: that.data.Jack_pots_val[that.data.random_number] })\n      //code\n      //运动结束设置可用抽奖的次数和激活状态设置可用\n      that.endset();\n    } else {//运动\n      //console.log(that.data.change_num)\n      if (that.data.change_num / that.data.Jack_pots_val.length + 1 < that.data.low_circle_num) {//正常转速\n        //console.log("正常转速")\n        that.data.use_speed = that.data.nor_speed\n      } else if (that.data.change_num / that.data.Jack_pots_val.length + 1 >= that.data.low_circle_num && that.data.change_num / that.data.Jack_pots_val.length + 1 <= that.data.base_circle_num + 1) { //减速圈\n        //console.log("减速圈")\n        that.data.use_speed = that.data.low_speed\n      } else if (that.data.change_num / that.data.Jack_pots_val.length + 1 > that.data.base_circle_num + 1) { //结束圈\n        //console.log("结束圈")\n        that.data.use_speed = that.data.end_speed\n      }\n      setTimeout(that.dong, that.data.use_speed);\n    }\n  },\n  //运动结束设置可用抽奖的次数和激活状态设置可用\n  endset: function () {\n    var that = this;\n    //是否在运动中，避免重复启动bug\n    that.setData({ is_play: false })\n    //可用抽奖的次数，可自定义设置\n    that.setData({ available_num: that.data.available_num - 1 });\n  },\n  //重置参数\n  reset: function () {\n    var that = this;\n    //转动开始时首次点亮的位置，可自定义设置\n    that.setData({ start_position: 0 });\n    //当前速度，与正常转速值相等\n    that.setData({ use_speed: that.data.nor_speed });\n    //中奖索引，也是随机数，也是结束圈停止的位置，这个值采用系统随机或者接口返回\n    that.setData({ random_number: 0 });\n    //变化计数，0开始，必须实例有12个奖项，基本是6圈，那么到结束这个值=6*12+random_number；同样change_num/12整除表示走过一整圈\n    that.setData({ change_num: 0 });\n  },\n})\n```\n';

    let article = towxImg.dealWithRichText(code);
    this.setData({ article });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})